function z = my_Obj(v)

  cd('C:\Users\amir\Desktop\optimization_codes')
  load('objmat');
  xr=S.xr;
  [x1,y1,z1]=ellipsoid(v(1),v(2),v(3),xr(1),xr(2),xr(3));
  xEllipsoid1 = reshape(x1,1,[])';
  yEllipsoid1 = reshape(y1,1,[])';
  zEllipsoid1 = reshape(z1,1,[])';
  teta1=deg2rad(v(7));
  phi1=deg2rad(v(8));
  xrotEllipsoid1 = [xEllipsoid1-v(1) yEllipsoid1-v(2) zEllipsoid1-v(3)]*[1 0 0;0 cos(teta1) -sin(teta1);0 sin(teta1) cos(teta1)];
  yrotEllipsoid1 = xrotEllipsoid1*[cos(phi1) 0 sin(phi1);0 1 0;-sin(phi1) 0 cos(phi1)];
  result1 = inhull([S.X1,S.X2,S.X3],[yrotEllipsoid1(:,1)+v(1), yrotEllipsoid1(:,2)+v(2), yrotEllipsoid1(:,3)+v(3)]);
  %%
[x2,y2,z2]=ellipsoid(v(4),v(5),v(6),xr(1),xr(2),xr(3));
xEllipsoid2 = reshape(x2,1,[])';
yEllipsoid2 = reshape(y2,1,[])';
zEllipsoid2 = reshape(z2,1,[])';
teta2=deg2rad(v(9));
phi2=deg2rad(v(10));
xrotEllipsoid2 = [xEllipsoid2-v(4) yEllipsoid2-v(5) zEllipsoid2-v(6)]*[1 0 0;0 cos(teta2) -sin(teta2);0 sin(teta2) cos(teta2)];
yrotEllipsoid2 = xrotEllipsoid2*[cos(phi2) 0 sin(phi2);0 1 0;-sin(phi2) 0 cos(phi2)];
result2 = inhull([S.X1,S.X2,S.X3],[yrotEllipsoid2(:,1)+v(4), yrotEllipsoid2(:,2)+v(5), yrotEllipsoid2(:,3)+v(6)]);
  %%
% [x3,y3,z3]=ellipsoid(v(11),v(12),v(13),xr(1),xr(2),xr(3));
% teta3=deg2rad(v(14));
% phi3=deg2rad(v(15));
% xEllipsoid3 = reshape(x3,1,[])';
% yEllipsoid3 = reshape(y3,1,[])';
% zEllipsoid3 = reshape(z3,1,[])';
% xrotEllipsoid3 = [xEllipsoid3-v(11) yEllipsoid3-v(12) zEllipsoid3-v(13)]*[1 0 0;0 cos(teta3) -sin(teta3);0 sin(teta3) cos(teta3)];
% yrotEllipsoid3 = xrotEllipsoid3*[cos(phi3) 0 sin(phi3);0 1 0;-sin(phi3) 0 cos(phi3)];
% % face3=delaunay(yrotEllipsoid2);
% % trisurf(face3,yrotEllipsoid3(:,1)+v(11),yrotEllipsoid3(:,2)+v(12),yrotEllipsoid3(:,3)+v(13),'FaceColor','flat'); 
% % axis equal
% result3 = inhull([S.X1,S.X2,S.X3],[yrotEllipsoid3(:,1)+v(11), yrotEllipsoid3(:,2)+v(12), yrotEllipsoid3(:,3)+v(13)]);
%%
%2 needles
vol1= S.result.*result1; vol2= S.result.*result2; 
vol4= S.result.*result1.*result2; 
In= S.result-vol1-vol2+vol4;
Out= result1-vol1+result2-vol2-(result1.*result2-vol4);
z=sum(5*In+Out);
%z=sum(In);

%3 needles
% vol1= S.result.*result1; vol2= S.result.*result2; vol3= S.result.*result3;
% vol4= S.result.*result1.*result2; 
% vol5= S.result.*result1.*result3; vol6= S.result.*result2.*result3;
% In= S.result-vol1-vol2-vol3+vol4+vol5+vol6; 
% Out=result1-vol1+result2-vol2+result3-vol3-(result1.*result2-vol4)-(result1.*result3-vol5)-(result2.*result3-vol6); %3 needles
% %z=sum(5*In+Out);
% z=sum(In);

% z= sum(S.result)+sum(result1)-2*sum(S.result.*result1)+sum(result2)-2*sum(S.result.*result2)...
%     +sum(result3)-2*sum(S.result.*result3)+5*sum(result1.*result2)+5*sum(result2.*result3)...
%     +5*sum(result1.*result3)-sum(result1.*result2.*result3); %3 needles
c= load('c.mat');
c=c.c+1;
S.count=S.count+1;
save('c.mat', 'c')
end